/*** manager ***/
package db;

import comm.Users;
import java.util.ArrayList;
import util.Utils;

public class DataBase {

  /**************************************************************************************************************
   * tem que chamar a communication para requisitar do banco de dados global caso esteja conectado
   **************************************************************************************************************/

  public static final int PRODUCTS = 1;
  public static final int SALES = 2;
  public static final int REQUISITIONS = 3;
  public static final int USERS = 4;
  private static boolean set = false;
  private static ArrayList<String[]> products = new ArrayList<String[]>();
  private static ArrayList<String[]> sales = new ArrayList<String[]>();
  private static ArrayList<String[]> req = new ArrayList<String[]>();
  private static ArrayList<String[]> users = new ArrayList<String[]>();

  private static void init() {
    products = new ArrayList<String[]>();
    products.add(new String[]{"4", "Cotrimoxazol", "Comprimidos", "R$ 05.95", "42"});
    products.add(new String[]{"8", "Eritromicina", "Comprimidos", "R$ 07.98", "43"});
    products.add(new String[]{"15", "Metronidazol", "Comprimidos", "R$ 09.90", "44"});
    products.add(new String[]{"16", "Dimenhidrinato", "Comprimidos", "R$ 10.00", "45"});
    products.add(new String[]{"23", "Ácido acetilsalícilico", "Comprimidos", "R$ 04.00", "46"});
    products.add(new String[]{"42", "Hidroclorotiazida", "Comprimidos", "R$ 15.00", "47"});
    products.add(new String[]{"48", "Metformina", "Comprimidos", "R$ 08.90", "48"});
    products.add(new String[]{"815", "Clorfeniramine", "Xarope", "R$ 06.50", "49"});
    products.add(new String[]{"1516", "Prometazina", "Xarope", "R$ 04.45", "50"});
    products.add(new String[]{"1623", "Cetoconazol", "Shampoo", "R$ 03.60", "51"});
    products.add(new String[]{"2342", "Piretrinas", "Shampoo", "R$ 05.25", "52"});
    products.add(new String[]{"424", "Iodopovidona", "Pomada", "R$ 06.30", "53"});
    products.add(new String[]{"415", "Hidrocortisona", "Pomada", "R$ 08.00", "54"});
    products.add(new String[]{"816", "Cloranfenicol", "Pomada", "R$ 06.50", "55"});
    products.add(new String[]{"1523", "Tetraciclina", "Pomada", "R$ 07.90", "56"});
    products.add(new String[]{"1642", "Fenilefrina", "Solução Nasal", "R$ 06.30", "57"});
    products.add(new String[]{"234", "Betametasona", "Creme", "R$ 04.20", "58"});
    products.add(new String[]{"428", "Etofenamato", "Creme", "R$ 03.30", "59"});
    products.add(new String[]{"423", "Sulfadiazina argentica", "Creme", "R$ 08.15", "60"});
    products.add(new String[]{"842", "Cetoconazol", "Creme", "R$ 05.60", "61"});
    products.add(new String[]{"154", "Clotrimazol", "Creme", "R$ 05.00", "62"});
    products.add(new String[]{"168", "Betametasona", "Creme", "R$ 06.60", "63"});

    sales = new ArrayList<String[]>();
    sales.add(new String[]{"4", "Cotrimoxazol", "10", "R$ 59.50", "13/04/09 14:00"});
    sales.add(new String[]{"26", "Cotrimoxazol", "150", "R$ 595.00", "13/04/09 16:00"});
    sales.add(new String[]{"28", "Piretrinas", "10", "R$ 52.50", "14/04/09 14:00"});
    sales.add(new String[]{"49", "Piretrinas", "30", "R$ 525.00", "14/04/09 16:00"});
    sales.add(new String[]{"54", "Fenilefrina", "25", "R$ 63.00", "15/04/09 14:00"});
    sales.add(new String[]{"77", "Fenilefrina", "250", "R$ 630.00", "15/04/09 16:00"});
    sales.add(new String[]{"79", "Cetoconazol", "70", "R$ 56.00", "16/04/09 14:00"});
    sales.add(new String[]{"86", "Cetoconazol", "42", "R$ 89.00", "16/04/09 16:00"});
    sales.add(new String[]{"89", "Piretrinas", "34", "R$ 456.00", "16/04/09 18:00"});
    sales.add(new String[]{"88", "Piretrinas", "75", "R$ 790.00", "17/04/09 16:00"});
    sales.add(new String[]{"90", "Piretrinas", "8", "R$ 152.00", "18/04/09 13:00"});
    sales.add(new String[]{"91", "Cetoconazol", "9", "R$ 30.00", "18/04/09 16:00"});
    sales.add(new String[]{"92", "Cetoconazol", "13", "R$ 510.00", "18/04/09 11:00"});
    sales.add(new String[]{"93", "Cetoconazol", "56", "R$ 240.00", "19/04/09 12:00"});
    sales.add(new String[]{"94", "Cetoconazol", "123", "R$ 63.00", "19/04/09 16:00"});
    sales.add(new String[]{"95", "Cetoconazol", "49", "R$ 30.00", "20/04/09 14:00"});
    sales.add(new String[]{"96", "Cetoconazol", "94", "R$ 660.00", "21/04/09 15:00"});
    sales.add(new String[]{"98", "Cetoconazol", "45", "R$ 463.00", "21/04/09 16:00"});
    sales.add(new String[]{"98", "Cetoconazol", "94", "R$ 512.00", "21/04/09 17:00"});
    sales.add(new String[]{"99", "Cetoconazol", "46", "R$ 49.00", "21/04/09 20:00"});


    req = new ArrayList<String[]>();
    req.add(new String[]{"4", "Cotrimoxazol", "10", "13/04/09 14:00"});
    req.add(new String[]{"7", "Piretrinas", "20", "13/04/09 14:00"});
    req.add(new String[]{"28", "Piretrinas", "10", "14/04/09 14:00"});
    req.add(new String[]{"54", "Fenilefrina", "25", "15/04/09 14:00"});
    req.add(new String[]{"88", "Piretrinas", "75", "17/04/09 16:00"});
    req.add(new String[]{"91", "Cetoconazol", "9", "18/04/09 16:00"});

    users = new ArrayList<String[]>();
    users.add(new String[]{"1", Users.TYPE_MANAGER, "admin", "admin"});
    users.add(new String[]{"2", Users.TYPE_MANAGER, "hugo", "senha"});
    users.add(new String[]{"3", Users.TYPE_MANAGER, "eduardo", "senha"});
    users.add(new String[]{"4", Users.TYPE_MANAGER, "parra", "senha"});
    users.add(new String[]{"5", Users.TYPE_VENDER, "vender", "vender"});

    set = true;
  }

  private static ArrayList getList(int type) {
    if (!set) {
      init();
    }
    ArrayList list;
    switch (type) {
      case PRODUCTS:
        list = products;
        break;
      case SALES:
        list = sales;
        break;
      case REQUISITIONS:
        list = req;
        break;
      case USERS:
        list = users;
        break;
      default:
        list = products;
        break;
    }
    return list;
  }

  public static String[][] readData(int queryType) {
    return Utils.listToArray(getList(queryType));
  }

  public static String[] readData(int column, String q, int queryType) {
    ArrayList list = getList(queryType);
    if(column < 0 || column > list.size() - 1) return null;
    for (int i = 0; i < list.size(); i++) {
      String[] s = (String[]) list.get(i);
      if (s[column].equals(q)) {
        return s;
      }
    }
    return null;
  }

  public static boolean writeData(int queryType, String[] query) {
    return write(getList(queryType), query);
  }

  public static boolean deleteData(int queryType, String[] query) {
    return delete(getList(queryType), query);
  }

  public static boolean deleteData(int column, String q, int queryType) {
    String[] s = readData(column, q, queryType);
    if (s == null) return false;
    return deleteData(queryType, s);
  }

  private static boolean write(ArrayList<String[]> list, String[] q) {
    if(q == null) return false;
    delete(list, q);
    list.add(q);
    return true;
  }

    private static boolean delete(ArrayList<String[]> list, String[] q) {
    if(q == null) return false;
    if (q.length != list.get(0).length) {
      return false;
    }
    // update
    for (int i = 0; i < list.size(); i++) {
      if (list.get(i)[0].equals(q[0])) {
        list.remove(i);
      }
    }
    return true;
  }
}
